#include "tcpsession.h"

TcpSession::TcpSession()
{
    connect(this, &TcpSession::readyRead, this, &TcpSession::SlotRead);
    //connect(this, &TcpSession::disconnected, this, &TcpSession::deleteLater);
    connect(this, &TcpSession::disconnected, this, &TcpSession::SlotDisconnect);
    connect(this, &TcpSession::SignalDoDisconnect, this, &TcpSession::SlotDoDisconnect);
}

TcpSession::~TcpSession()
{
    qDebug() << "TcpSession::~TcpSession";
    disconnect(this, &TcpSession::readyRead, this, &TcpSession::SlotRead);
    //disconnect(this, &TcpSession::disconnected, this, &TcpSession::deleteLater);
    disconnect(this, &TcpSession::disconnected, this, &TcpSession::SlotDisconnect);
    disconnect(this, &TcpSession::SignalDoDisconnect, this, &TcpSession::SlotDoDisconnect);
}

void TcpSession::Disconnect()
{
    emit SignalDoDisconnect();
}

void TcpSession::SlotDoDisconnect()
{
    disconnectFromHost();
}

size_t TcpSession::Write(const char* buf, size_t len)
{
    return this->write(buf, len);
}

void TcpSession::SlotRead()
{
    QByteArray buff;
    buff = this->readAll();
    if(buff.length() <= 0) return;

    emit SignalRead(buff);
}

void TcpSession::SlotDisconnect()
{
    emit SignalDisconnect(m_IPPort);
}

QString TcpSession::getIPPort() const
{
    return m_IPPort;
}

void TcpSession::setIPPort(const QString ip, int port)
{
    m_IPPort = QString("%1:%2").arg(ip).arg(port);
}
